import java.util.Arrays;

public class myStack2 {
    //用单链表实现栈(尾插尾删)
    public int val;
    static class listNode{
        public int val;
        public listNode next;
        public listNode(int val){
            this.val=val;
        }
    }
    public listNode head;
    //入栈
    public void push(int val){
        listNode node=new listNode(val);
        if(empty()){
            head=node;
        }else{
            listNode pcur=head;
            while(pcur.next!=null){
                pcur=pcur.next;
            }
            pcur.next=node;
        }
    }
    //出栈
    public int pop(){
        int value=0;
        if(empty()){
            return -1;
        }else{
            if(head.next==null){
                value=head.val;
                head=null;
            }else{
                listNode pcur=head;
                while (pcur.next!=null){
                    pcur=pcur.next;
                }
                value=pcur.val;
                listNode pcurr=head;
                listNode ppcur=pcurr.next;
                while(ppcur.next!=null){
                    pcurr=ppcur;
                    ppcur=ppcur.next;
                }
                pcurr.next=null;
            }

        }
        return value;
    }
    //获取栈顶元素
    public int peek(){
        int value=0;
        if(empty()){
           return -1;
        }else {
            listNode pcur=head;
            while (pcur.next!=null){
                pcur=pcur.next;
            }
            value= pcur.val;
        }
      return value;
    }

    //判空
    public boolean empty(){
        return head==null;
    }


}
